// pages/myOrder/myOrder.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_type: 2,
    page: 1,
    limit: 10,
    total_page: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var types = options.types;
    if (types) {
      this.setData({
        order_type: types
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page: 1
    })
    this.getOrderLIst();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  // 订单分类
  changeType: function (e) {
    var types = e.currentTarget.dataset.type;
    this.setData({
      page: 1,
      order_type: types
    })
    this.getOrderLIst();
  },

  // 获取订单列表
  getOrderLIst: function () {
    var params = new Object()
    var that = this;
    params.page = this.data.page;
    params.limit = this.data.limit;
    params.type = this.data.order_type;

    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsorder/list',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.paging;
          var totalPage = paging.total_page;
          var orderList = res.data.data.list;

          if (that.data.page == 1) {
            var itemData = orderList;
          } else {
            var itemData = that.data.product.concat(orderList)
          }

          that.setData({
            total_page: totalPage,
            product: itemData
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },

  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.getOrderLIst();
  },

  //刷新页面
  resetData: function () {
    var that = this
    that.setData({
      page: 1,
      hasMore: true
    });
  },
  //页面下拉刷新
  onPullDownRefresh: function () {
    var that = this;

    setTimeout(function () {
      that.resetData()
    }, 1000)
    wx.stopPullDownRefresh();
  },

  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },
  //订单详情
  orderDetails: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?oid=' + oid
    })
  },

  // 订单支付
  orderPay: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否确认支付',
      success: function (res) {
        if (res.confirm) {

          that.wxPayOrder(oid);
        }
      }
    })
  },

  // 订单支付
  //支付接口
  wxPayOrder: function (oid) {
    var params = new Object()
    var that = this;
    params.oid = oid;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'payment/getWechatInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {


          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              that.onShow();
            },
            fail: function (error) {
              util.showError("支付失败");
            }
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },

  //确认收货
  receiveOrder: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: oid },
            url: app.globalData.base_url + 'order/receipt',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },

  //取消取货
  cancelPickUp: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否取消取货',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: oid },
            url: app.globalData.base_url + 'order/cancelPick',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },

  //打开柜门
  openDoor: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否打开柜门取货?',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: oid },
            url: app.globalData.base_url + 'order/pickup',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                util.showError("柜门已打开");
                that.onShow();
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("请检查网络");
            }
          })
        }
      }
    })
  },

  //开发票
  getInvoice: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '/pages/invoice/invoice?oid=' + oid
    })
  }

})